﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SgAutoArchiveBrrDal;
using SgAutoArchiveBrrDal.Linq;

namespace SgAutoArchiveSp.Dialog
{
    public partial class Dialog_UserInfo : DevExpress.XtraEditors.XtraForm
    {
        private T_USER _en;
        public bool _bOK = false;
        public Dialog_UserInfo(T_USER en)
        {
            InitializeComponent();
            if(en == null)
            {
                this.Text = "添加用户";
                cmb_Status.SelectedIndex = cmb_Stat.SelectedIndex = cmb_User.SelectedIndex = 1;
            }
            else
            {
                _en = en;
                this.Text = "修改用户: " + en.U_RNAME;
                txt_Pwd.Text = txt_Pwd2.Text = "";
                txt_Pwd.Enabled = txt_Pwd2.Enabled = false;
                BindControl(_en);
            }
        }

        public void BindControl(T_USER en)
        {
            txt_UName.Text = en.U_NAME;
            txt_URName.Text = en.U_RNAME;
            cmb_Stat.SelectedIndex = (en.U_I01 == null ? 0 : en.U_I01.Value);
            cmb_Status.SelectedIndex = en.U_STATUS;
            cmb_User.SelectedIndex = (en.U_I02 == null ? 0 : en.U_I02.Value);
        }

        public void BindEntity()
        {
            if (_en == null)
                _en = new T_USER();
            _en.U_NAME = txt_UName.Text;
            if(txt_Pwd.Enabled)
                _en.U_PWD = SgArchiveCommons.SGText.MD5(txt_Pwd.Text);
            _en.U_RNAME = txt_URName.Text;
            _en.U_STATUS = cmb_Status.SelectedIndex;
            _en.U_I01 = cmb_Stat.SelectedIndex;
            _en.U_I02 = cmb_User.SelectedIndex;
        }

        public bool Vaild()
        {
            if (txt_UName.Text.Trim() == "")
                MessageBox.Show("用户名不能为空!");
            else if (txt_URName.Text.Trim() == "")
                MessageBox.Show("姓名不能为空!");
            else if (txt_Pwd.Text.Trim() == "" || txt_Pwd2.Text.Trim() == "")
                MessageBox.Show("密码不能为空!");
            else if (txt_Pwd.Text != txt_Pwd2.Text)
                MessageBox.Show("两次密码输入不正确!");
            else
                return true;
            return false;
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            if (!Vaild())
                return;
            BindEntity();
            int value = -1;
            if (_en.U_ID > 0)
                ///那么就是修改
                value = T_USER_Dal.Modify_User(_en);
            else
                ///那么就是添加
                value = T_USER_Dal.Add_User(_en);
            if (value == -1)
                MessageBox.Show("保存失败!");
            else if (value == 0)
                MessageBox.Show("有重复的用户名!");
            else
            {
                MessageBox.Show("保存成功!");
                _bOK = true;
                this.Close();
            }
        }
    }
}